using JGSY.CMS.LowCode.Platform.Application.DTOs;
using JGSY.CMS.LowCode.Platform.Domain.Enums;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Application.Interfaces
{
    /// <summary>
    /// 工作流应用服务接口
    /// 提供工作流管理相关的业务逻辑操作
    /// </summary>
    public interface IWorkflowAppService
    {
        /// <summary>
        /// 获取所有工作流
        /// </summary>
        /// <returns>工作流列表</returns>
        Task<IEnumerable<WorkflowDto>> GetAllAsync();

        /// <summary>
        /// 根据ID获取工作流
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <returns>工作流信息</returns>
        Task<WorkflowDto?> GetByIdAsync(long id);

        /// <summary>
        /// 根据标识符获取工作流
        /// </summary>
        /// <param name="identifier">工作流标识符</param>
        /// <returns>工作流信息</returns>
        Task<WorkflowDto?> GetByIdentifierAsync(string identifier);

        /// <summary>
        /// 根据分类获取工作流列表
        /// </summary>
        /// <param name="category">工作流分类</param>
        /// <returns>工作流列表</returns>
        Task<IEnumerable<WorkflowDto>> GetByCategoryAsync(string category);

        /// <summary>
        /// 获取活跃工作流
        /// </summary>
        /// <returns>活跃工作流列表</returns>
        Task<IEnumerable<WorkflowDto>> GetActiveWorkflowsAsync();

        /// <summary>
        /// 获取运行中的工作流
        /// </summary>
        /// <returns>运行中的工作流列表</returns>
        Task<IEnumerable<WorkflowDto>> GetRunningWorkflowsAsync();

        /// <summary>
        /// 分页获取工作流列表
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="name">工作流名称</param>
        /// <param name="category">工作流分类</param>
        /// <returns>分页结果</returns>
        Task<(IEnumerable<WorkflowDto> Items, int TotalCount)> GetPagedAsync(int pageIndex = 1, int pageSize = 20, string? name = null, string? category = null);

        /// <summary>
        /// 创建工作流
        /// </summary>
        /// <param name="dto">工作流数据</param>
        /// <returns>创建的工作流</returns>
        Task<WorkflowDto> CreateAsync(WorkflowDto dto);

        /// <summary>
        /// 更新工作流
        /// </summary>
        /// <param name="dto">工作流数据</param>
        /// <returns>更新的工作流</returns>
        Task<WorkflowDto?> UpdateAsync(WorkflowDto dto);

        /// <summary>
        /// 删除工作流
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <returns>是否删除成功</returns>
        Task<bool> DeleteAsync(long id);

        /// <summary>
        /// 启动工作流
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <param name="inputData">输入数据</param>
        /// <param name="userId">用户ID</param>
        /// <returns>工作流实例ID</returns>
        Task<long> StartWorkflowAsync(long id, Dictionary<string, object>? inputData = null, long? userId = null);

        /// <summary>
        /// 停止工作流
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <param name="reason">停止原因</param>
        /// <returns>是否停止成功</returns>
        Task<bool> StopWorkflowAsync(long id, string? reason = null);

        /// <summary>
        /// 暂停工作流
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <returns>是否暂停成功</returns>
        Task<bool> PauseWorkflowAsync(long id);

        /// <summary>
        /// 恢复工作流
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <returns>是否恢复成功</returns>
        Task<bool> ResumeWorkflowAsync(long id);

        /// <summary>
        /// 获取工作流实例
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <param name="status">状态筛选</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>实例列表</returns>
        Task<(IEnumerable<object> Items, int TotalCount)> GetWorkflowInstancesAsync(long id, string? status = null, int pageIndex = 1, int pageSize = 20);

        /// <summary>
        /// 获取工作流统计信息
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <param name="period">统计周期</param>
        /// <returns>统计信息</returns>
        Task<object> GetWorkflowStatisticsAsync(long id, string period = "month");

        /// <summary>
        /// 获取执行历史
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>执行历史</returns>
        Task<(IEnumerable<object> Items, int TotalCount)> GetExecutionHistoryAsync(long id, int pageIndex = 1, int pageSize = 20);

        /// <summary>
        /// 获取全局统计信息
        /// </summary>
        /// <param name="period">统计周期</param>
        /// <returns>全局统计信息</returns>
        Task<object> GetGlobalStatisticsAsync(string period = "month");

        /// <summary>
        /// 获取工作流性能统计
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <param name="period">统计周期</param>
        /// <returns>性能统计信息</returns>
        Task<object> GetWorkflowPerformanceStatisticsAsync(long id, string period = "month");

        /// <summary>
        /// 获取工作流趋势统计
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <param name="days">统计天数</param>
        /// <returns>趋势统计信息</returns>
        Task<object> GetWorkflowTrendStatisticsAsync(long id, int days = 30);

        /// <summary>
        /// 导出工作流
        /// </summary>
        /// <param name="id">工作流ID</param>
        /// <param name="format">格式</param>
        /// <returns>导出数据</returns>
        Task<string?> ExportWorkflowAsync(long id, string format = "json");

        /// <summary>
        /// 批量导出工作流为ZIP文件
        /// </summary>
        /// <param name="ids">工作流ID列表</param>
        /// <param name="format">导出格式</param>
        /// <returns>ZIP文件数据</returns>
        Task<byte[]> ExportWorkflowsAsZipAsync(long[] ids, string format = "json");

        /// <summary>
        /// 导入工作流
        /// </summary>
        /// <param name="jsonData">JSON数据</param>
        /// <param name="overwrite">是否覆盖同名工作流</param>
        /// <returns>导入的工作流</returns>
        Task<WorkflowDto?> ImportWorkflowAsync(string jsonData, bool overwrite = false);

        /// <summary>
        /// 从ZIP文件批量导入工作流
        /// </summary>
        /// <param name="zipData">ZIP文件数据</param>
        /// <param name="overwrite">是否覆盖同名工作流</param>
        /// <returns>批量导入结果</returns>
        Task<WorkflowBatchImportResult> ImportWorkflowsFromZipAsync(byte[] zipData, bool overwrite = false);

        /// <summary>
        /// 复制工作流
        /// </summary>
        /// <param name="id">源工作流ID</param>
        /// <param name="newName">新名称</param>
        /// <returns>复制的工作流</returns>
        Task<WorkflowDto?> CopyWorkflowAsync(long id, string newName);

        /// <summary>
        /// 清理过期的工作流实例
        /// </summary>
        /// <param name="daysToKeep">保留天数</param>
        /// <returns>清理的实例数</returns>
        Task<int> CleanupExpiredInstancesAsync(int daysToKeep = 30);

        /// <summary>
        /// 搜索工作流
        /// </summary>
        /// <param name="keyword">搜索关键字</param>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns>搜索结果</returns>
        Task<(IEnumerable<WorkflowDto> Items, int TotalCount)> SearchAsync(string keyword, int pageIndex = 1, int pageSize = 20);

    /// <summary>
    /// 将任务置为进行中
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <param name="userId">操作用户ID</param>
    /// <param name="comment">备注</param>
    Task<bool> StartTaskAsync(long taskId, long userId, string? comment = null);

    /// <summary>
    /// 完成任务并尝试推进到下一节点
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <param name="userId">操作用户ID</param>
    /// <param name="comment">备注</param>
    /// <param name="outputData">输出数据</param>
    Task<bool> CompleteTaskAsync(long taskId, long userId, string? comment = null, Dictionary<string, object>? outputData = null);

    /// <summary>
    /// 拒绝任务（实例将置为失败）
    /// </summary>
    /// <param name="taskId">任务ID</param>
    /// <param name="userId">操作用户ID</param>
    /// <param name="reason">原因</param>
    Task<bool> RejectTaskAsync(long taskId, long userId, string? reason = null);

    /// <summary>
    /// 推进实例到下一节点（当无需人工任务时）
    /// </summary>
    /// <param name="instanceId">实例ID</param>
    Task<bool> AdvanceInstanceAsync(long instanceId);

    /// <summary>
    /// 我的待办任务（含处理中）
    /// </summary>
    /// <param name="userId">用户ID</param>
    /// <param name="pageIndex">页索引</param>
    /// <param name="pageSize">页大小</param>
    /// <param name="priority">按优先级过滤</param>
    /// <param name="dueBefore">在该截止时间前</param>
    /// <param name="keyword">任务名/节点名模糊</param>
    Task<(IEnumerable<object> Items, int TotalCount)> GetMyPendingTasksAsync(long userId, int pageIndex = 1, int pageSize = 20, int? priority = null, System.DateTime? dueBefore = null, string? keyword = null);

    /// <summary>
    /// 我发起的实例
    /// </summary>
    /// <param name="userId">用户ID</param>
    /// <param name="status">实例状态筛选</param>
    /// <param name="pageIndex">页索引</param>
    /// <param name="pageSize">页大小</param>
    Task<(IEnumerable<object> Items, int TotalCount)> GetMyInitiatedInstancesAsync(long userId, WorkflowInstanceStatus? status = null, int pageIndex = 1, int pageSize = 20);

    /// <summary>
    /// 刷新超时任务状态（将已过期未完成的任务标记为超时）
    /// </summary>
    Task<int> RefreshOverdueTasksAsync();

    /// <summary>
    /// 获取工作流实例详情
    /// </summary>
    /// <param name="instanceId">实例ID</param>
    /// <returns>实例详情</returns>
    Task<object?> GetWorkflowInstanceDetailAsync(long instanceId);

    /// <summary>
    /// 停止工作流实例
    /// </summary>
    /// <param name="instanceId">实例ID</param>
    /// <param name="reason">停止原因</param>
    /// <returns>是否成功</returns>
    Task<bool> StopWorkflowInstanceAsync(long instanceId, string? reason = null);

    /// <summary>
    /// 重试工作流实例
    /// </summary>
    /// <param name="instanceId">实例ID</param>
    /// <returns>是否成功</returns>
    Task<bool> RetryWorkflowInstanceAsync(long instanceId);

    /// <summary>
    /// 批量操作工作流实例
    /// </summary>
    /// <param name="instanceIds">实例ID列表</param>
    /// <param name="operation">操作类型</param>
    /// <param name="reason">操作原因</param>
    /// <returns>操作结果</returns>
    Task<BatchOperationResult> BatchOperateInstancesAsync(List<long> instanceIds, string operation, string? reason = null);

    // === Workflow Versioning ===
    /// <summary>
    /// 创建工作流版本快照
    /// </summary>
    Task<WorkflowVersionDto> CreateVersionAsync(long workflowId, string? tag = null, string? description = null, long? userId = null);

    /// <summary>
    /// 获取工作流的所有版本（按版本号倒序）
    /// </summary>
    Task<IEnumerable<WorkflowVersionDto>> GetVersionsAsync(long workflowId);

    /// <summary>
    /// 获取指定版本
    /// </summary>
    Task<WorkflowVersionDto?> GetVersionAsync(long workflowId, int version);

    /// <summary>
    /// 回滚到指定版本（会生成一个新的版本快照，描述为回滚自X）
    /// </summary>
    Task<bool> RollbackToVersionAsync(long workflowId, int version, long? userId = null, string? reason = null);
    }
}
